<?php

/*

Process Found Wild Pets (fishing_catch_pet.pro.php)

*/
ob_start();
include "global.inc.php";

$randomRun = rand(1,100);
$randomCatch = rand(1,100);

$findfish = fetch("SELECT * FROM fish_pets2 WHERE userid = '$userid' AND game = '$game'");
if (!$findfish[id])
{
	die(header(error("fish_for_pets.php?game=$game","You are not fishing right now.")));
}

if (($findfish[found] != "pet") AND ($findfish[found] != "pet2"))
{
	die(header(error("fishing_for_pets.php?game=$game","You have not found a pet yet.")));
}

$findPet = fetch("SELECT * FROM pets2 WHERE name = '$findfish[found_what]' AND game = '$game'");

if ($act == "run")
{
	mysql_query("UPDATE fish_pets2 SET found = '' WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE fish_pets2 SET found_what = '' WHERE userid = '$userid' AND game = '$game'");
	die(header(error("fishing_for_pets.php?game=$game","You have run from the wild $findPet[name]!")));
}

if ($act == "catch")
{
	mysql_query("UPDATE fish_pets2 SET found = 'pet2' WHERE userid = '$userid' AND game = '$game'");
	die(header(error("fishing_for_pets.php?game=$game","Hurry, try to catch the $findPet[name]!")));
}

if ($act == "bait")
{
	$findItem = fetch("SELECT * FROM usersitems2 WHERE id = '$throw_food' AND owner = '$userid' AND game = '$game'");
	$findItem = fetch("SELECT * FROM items2 WHERE id = '$findItem[item_id]' AND game = '$game'");
	if ($findItem[item_type] != "food")
	{
		die(header(error("fishing_for_pets.php?game=$game","You can only use your food items for bait!")));
	}
	if (!$lowerChance)
	{
		$lowerChance = rand(1,6);
	}
	mysql_query("UPDATE fish_pets2 SET chance_of_running=$findfish[chance_of_running]-$lowerChance WHERE userid = '$userid' AND game = '$game'");
	mysql_query("DELETE FROM usersitems2 WHERE id = '$throw_food' AND game = '$game'");

	if ($randomRun < ($findfish[chance_of_running]-$lowerChance))
	{
		mysql_query("UPDATE fish_pets2 SET found = '' WHERE userid = '$userid' AND game = '$game'");
		mysql_query("UPDATE fish_pets2 SET found_what = '' WHERE userid = '$userid' AND game = '$game'");
		mysql_query("UPDATE fish_pets2 SET found_what2=0 WHERE userid = '$userid' AND game = '$game'");
		mysql_query("UPDATE fish_pets2 SET chance_of_running=30 WHERE userid = '$userid' AND game = '$game'");
		die(header(error("fishing_for_pets.php?game=$game","You threw your $findItem[item_name] at the $findPet[name]!<br>The $findPet[name] got mad and ran away!")));
	}

	die(header(error("fishing_for_pets.php?game=$game","You used your $findItem[item_name] to bait the $findPet[name]!")));
}

if ($act == "net")
{
	if ($net == "basic")
	{
		if ($findfish[basic_nets] <= 0)
		{
			die(header(error("fishing_for_pets.php?game=$game","You do not have any $net nets.")));
		}

		$higherChance = 4;
		$chance2Catch = 20;
		mysql_query("UPDATE fish_pets2 SET basic_nets=$findfish[basic_nets]-1 WHERE userid = '$userid' AND game = '$game'");
	}
	if ($net == "electric")
	{
		if ($findfish[elec_nets] <= 0)
		{
			die(header(error("fishing_for_pets.php?game=$game","You do not have any $net nets.")));
		}

		$higherChance = 8;
		$chance2Catch = 25;
		mysql_query("UPDATE fish_pets2 SET elec_nets=$findfish[elec_nets]-1 WHERE userid = '$userid' AND game = '$game'");
	}
	if ($net == "magic")
	{
		if ($findfish[magic_nets] <= 0)
		{
			die(header(error("fishing_for_pets.php?game=$game","You do not have any $net nets.")));
		}

		$higherChance = 16;
		$chance2Catch = 33;
		mysql_query("UPDATE fish_pets2 SET magic_nets=$findfish[magic_nets]-1 WHERE userid = '$userid' AND game = '$game'");
	}
	if ($findfish[chance_of_winning] < 0)
	{
		$chance = (($findfish[chance_of_winning] * -1) - $higherChance) * -1;
	}
	else
	{
		$chance = $findfish[chance_of_winning] + $higherChance;
	}
	mysql_query("UPDATE fish_pets2 SET chance_of_running='$chance' WHERE userid = '$userid' AND game = '$game'");
	if ($randomRun < ($findfish[chance_of_running] + $higherChance))
	{
		mysql_query("UPDATE fish_pets2 SET found = '' WHERE userid = '$userid' AND game = '$game'");
		mysql_query("UPDATE fish_pets2 SET found_what = '' WHERE userid = '$userid' AND game = '$game'");
		mysql_query("UPDATE fish_pets2 SET found_what2=0 WHERE userid = '$userid' AND game = '$game'");
		mysql_query("UPDATE fish_pets2 SET chance_of_running=30 WHERE userid = '$userid' AND game = '$game'");

		die(header(error("fishing_for_pets.php?game=$game","You threw a $net net at the $findPet[name]!<br>The $findPet[name] got mad and ran away!")));
	}
	$difficulty = $findfish[find_what2] * 3;
	if ($randomCatch < ($chance2Catch - $difficulty))
	{
		mysql_query("UPDATE fish_pets2 SET found = 'caught_pet' WHERE userid = '$userid' AND game = '$game'");
		die(header(error("fishing_for_pets.php?game=$game&net=$net","You threw a $net net at the $findPet[name]!<br>The pet was caught!!!!!")));
	}
	die(header(error("fishing_for_pets.php?game=$game&net=$net","You threw a $net net at the $findPet[name]!<br>The $findPet[name] got angry!")));
}

header("Location: fishing_for_pets.php?game=$game");
?>